//
//  LBSutroFilter.mm
//  OpenGLGame
//
//  Created by Nguyen Cong Huy on 4/3/13.
//  Copyright (c) 2013 UIT. All rights reserved.
//

#include "LBSutroFilter.h"
#include "LBSaturationFilter.h"
#include "LBOverlayTextureFilter.h"
#include "LBOverlayColorFilter.h"
#include "LBExclusionColorFilter.h"
#include "LBBrightnessFilter.h"
#include "LBContrastFilter.h"
#include "LBUtils.h"
#include "LBToneCurveFilter.h"
#import "AcvFileReader.h"
#include "LBLevelFilter.h"
#include "LBTexture.h"
#import "OCImage.h"
#include "LBColorBurnTextureFilter.h"
#include "LBSoftlightTextureFilter.h"

LBSutroFilter::LBSutroFilter(){
    
    brightness = new LBBrightnessFilter();
    brightness->setBrightness(0.07);

    contrast = new LBContrastFilter();
    contrast->setContrast(1.1);
    
    saturation = new LBSaturationFilter();
    saturation->setSaturation(0.6);
    
    AcvFileReader* sutro1Acv = [[AcvFileReader alloc] initWithACV:@"sutro1-curves"];
    
    toneCurves1 = new LBToneCurveFilter();
    toneCurves1->setCurveImageData(sutro1Acv.toneCurveByteArray);
    
    [sutro1Acv release];
    
    saturation2 = new LBSaturationFilter();
    saturation2->setSaturation(0.8);
    
    brightness2 = new LBBrightnessFilter();
    brightness2->setBrightness(-0.03);
    
    contrast2 = new LBContrastFilter();
    contrast2->setContrast(1.05);
 
    level = new LBLevelFilter();
    level->setGammaR(1.16);
    level->setGammaG(1.18);
    level->setMaxOutputG(0.984);
    level->setGammaB(1.13);
    level->setMaxOutputB(0.984);
    
    contrast3 = new LBContrastFilter();
    contrast3->setContrast(1.07);
    
    AcvFileReader* sutro2Acv = [[AcvFileReader alloc] initWithACV:@"sutro2-curves"];
    
    toneCurves2 = new LBToneCurveFilter();
    toneCurves2->setCurveImageData(sutro2Acv.toneCurveByteArray);
    
    [sutro2Acv release];
    
    OCImage* ocImg = [[OCImage alloc] initWithFileName:@"sutro_gradient.png"];
    LBTexture* blendTexture = new LBTexture([ocImg imageData], [ocImg width], [ocImg height], [ocImg byteInAPixel]);
    blendTexture->setLocationAndScale(0.0f, 0.0f, 1.0f, 1.0f);
    [ocImg release];
    
    colorBurnGradient = new LBColorBurnTextureFilter();
    colorBurnGradient->setBlendTexture(blendTexture);
    colorBurnGradient->setOpactiy(0.5);
    
//    OCImage* ocImg = [[OCImage alloc] initWithFileName:@"blend.png"];
//    LBTexture* blendTexture = new LBTexture([ocImg imageData], [ocImg width], [ocImg height], [ocImg byteInAPixel]);
//    blendTexture->setLocationAndScale(0.0f, 0.0f, 1.0f, 1.0f);
//    [ocImg release];
//    
//    softlightGradient = new LBSoftlightTextureFilter();
//    softlightGradient->setBlendTexture(blendTexture);
//    softlightGradient->setOpactiy(0.3);

    
    brightness->addTarget(contrast)->addTarget(saturation)->addTarget(toneCurves1)->addTarget(saturation2)->addTarget(brightness2)->addTarget(contrast2)->addTarget(level)->addTarget(contrast3)->addTarget(colorBurnGradient)->addTarget(toneCurves2);
    firstFilter = brightness;
}

LBSutroFilter::~LBSutroFilter(){
    outputTexture = 0;
    DELETE_SAFELY(brightness);
    DELETE_SAFELY(contrast);
    DELETE_SAFELY(saturation);
    DELETE_SAFELY(toneCurves1);
    DELETE_SAFELY(saturation2);
    DELETE_SAFELY(brightness2);
    DELETE_SAFELY(contrast2);
    DELETE_SAFELY(level);
    DELETE_SAFELY(contrast3);
    DELETE_SAFELY(toneCurves2);
    DELETE_SAFELY(colorBurnGradient);
}

void LBSutroFilter::setBlendTexture(LBTexture* blendTexture){
//    gradientOverlay->setBlendTexture(blendTexture);
}